/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

import java.util.LinkedList;

public class Attribute extends ItemDefinition {

	private LinkedList<Locus> locusList = new LinkedList<Locus>();
	private Type type = null;
	private Redefinition redefinition = null;
	private Properties properties = null;
	
	private Attribute(){
		super(null);
	}

    public Attribute(LinkedList<Locus> locusList, Type type, Redefinition redefinition, Properties properties) throws DliException {
    	super(null);
		if (locusList == null){
			throw new DliException();
		}
		this.locusList = locusList;
		this.type = type;
		this.properties = properties;
		this.redefinition = redefinition;
    }
	
	public static ItemDefinition newInstance(String string) throws DliParseException {
		Attribute attribute = new Attribute();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Attribute");
		}
		if (!string.substring(0, position).equals("attr")) {
			throw new DliParseException("["+string+"] is not valid in Attribute");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		String type = null;
		while (positionComma != -1) {
			position = string.indexOf("(");
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in Attribute");
			}
			type = string.substring(0, position);
			if (type.equals("$locus")){
				attribute.locusList.add(Locus.newInstance(string.substring(0, positionComma)));
			} else if (type.equals("$type")){
				attribute.type = Type.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$redefinition")){
				attribute.redefinition = Redefinition.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$properties")){
				attribute.properties = Properties.newInstance(string.substring(0, positionComma));
			} else {
				throw new DliParseException("["+string+"] is not valid in Attribute");
			}
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Attribute");
		}
		type = string.substring(0, position);
		if (type.equals("$locus")){
			attribute.locusList.add(Locus.newInstance(string));
		} else if (type.equals("$type")){
			attribute.type = Type.newInstance(string);
		} else if (type.equals("$redefinition")){
			attribute.redefinition = Redefinition.newInstance(string);
		} else if (type.equals("$properties")){
			attribute.properties = Properties.newInstance(string);
		} else {
			throw new DliParseException("["+string+"] is not valid in Attribute");
		}
		return attribute;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("attr(");
		boolean first = true;
		for (Locus locus : locusList){
			if (first) first = false;
			else sb.append(",");
			sb.append(locus.toString());
		}
		if (type != null){
			if (locusList.size()>0) {
				sb.append(",");
			}
			sb.append(type.toString());
		}
		if (redefinition != null){
			sb.append(",");
			sb.append(redefinition.toString());
		}
		if (properties != null){
			sb.append(",");
			sb.append(properties.toString());
		}
		sb.append(")");
		return sb.toString();
		
	}
	
}
